﻿/*  ----------------------------------------------------------------------------
 *  EuphoricNation
 *  ----------------------------------------------------------------------------
 *  EuphoricEngine v1.0
 *  ----------------------------------------------------------------------------
 *  File:       Component.cs
 *  Author:     Shane-PC\Shane
 *  
 *  Credits:    Sean James's Game Engine Tutorial (See Below)
 *  
 *  Tutorial:   'http://www.innovativegames.net/blog/2009/10/20/game-engine-tutorial-part-iii-component/'
 *  
 *  Notes:      * Component Began At First Post Of The Tutorial Link Above
 *              * Component Modified/Expanded To Work With My Version Of Game Engine!
 *  
 *  ----------------------------------------------------------------------------
 */

using System;
using System.Collections.Generic;

namespace EuphoricEngine
{
    public class Component
    {
        private string _name;
        private bool _loaded;
        private GameScreen _parent;

        private int _drawOrder = 0;

        public Component()
        {
            GenerateUniqueName();
        }

        public virtual void Update()
        {
        }

        public virtual void Draw()
        {
        }

        public void LoadComponent()
        {
            if (!_loaded)
                Load();

            _loaded = true;
        }

        protected virtual void Load()
        {
        }

        public GameScreen Parent
        {
            get { return _parent; }
            set
            {
                if (_parent == value)
                    return;

                if (_parent != null)
                    _parent.RemoveComponent(this);

                _parent = value;

                if (value != null)
                    _parent.AddComponent(this);
            }
        }

        public int DrawOrder
        {
            get { return _drawOrder; }
            set
            {
                _drawOrder = value;

                if (Parent != null)
                    Parent.PutComponentInOrder(this);
            }
        }

        public bool Loaded
        {
            get { return _loaded; }
        }

        public string Name
        {
            get { return _name; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    throw new Exception("Component name must not be null and it's character length should be greater than 0");
                else
                    _name = value;
            }
        }

        static Dictionary<Type, int> componentTypeCounts = new Dictionary<Type, int>();
        private void GenerateUniqueName()
        {
            Type t = this.GetType();

            if (!componentTypeCounts.ContainsKey(t))
                componentTypeCounts.Add(t, 0);

            componentTypeCounts[t]++;

            _name = t.Name + componentTypeCounts[t];
        }
    }
}
